package cn.tinlan.shopcoin.app.mine;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/12/26. 18:55
 * 佛祖🙏，永无bug。
 */
public class PointActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.point_fun_title)
    TitleBar titleBar;
    @BindView(R.id.point_code_et)
    EditText codeET;
    @BindView(R.id.point_point_et)
    EditText ppET;
    @BindView(R.id.given_point_btn)
    Button gvienPBtn;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_given_point;
    }

    @Override
    public void initView() {
        super.initView();

        drawableDefaultBar();

        initTitleBar();
        bindSubViewClickEvents();
    }

    private void bindSubViewClickEvents() {
        gvienPBtn.setOnClickListener(this);
    }

    private void initTitleBar() {
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
    }


    private void sendPoint() {

        if (gvienPBtn.isSelected()) return;

        String r_code = codeET.getText().toString().trim();
        if (r_code.isEmpty()){
            ToastUtils.show("请填写您的Fans码");
            return;
        }
        String point = ppET.getText().toString().trim();
        if (point.isEmpty()) {
            ToastUtils.show("请输入积分");
            return;
        }

        gvienPBtn.setSelected(!gvienPBtn.isSelected());

        EasyHttp.post(Constants.KoiSendPoint)
                .params("r_code",r_code)
                .params("account",point)
                .params("user_id", SPUtils.getUserID(PointActivity.this))
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");
                        gvienPBtn.setSelected(false);
                    }

                    @Override
                    public void onSuccess(String s) {

                        gvienPBtn.setSelected(false);
                        ToastUtils.show("转赠成功");
                    }
                }) {
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.given_point_btn:
                sendPoint();
                break;
        }
    }
}
